//
//  LSFNavigationController.swift
//  hoonpay
//
//  Created by 李胜锋 on 2017/8/1.
//  Copyright © 2017年 lishengfeng. All rights reserved.
//

import UIKit

class LSFNavigationController: UINavigationController {
    
    var popRecognizer:UIPanGestureRecognizer!


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        //禁用系统原生的侧滑返回功能
        let gesture = self.interactivePopGestureRecognizer
        gesture?.isEnabled = false
        
        let targets = gesture?.value(forKey: "_targets") as? [NSObject]
        let targetObj = targets?.first
        
        let target = targetObj?.value(forKey: "_target")
        let action = Selector(("handleNavigationTransition:"))
        
        popRecognizer = UIPanGestureRecognizer.init(target: target,
                                                    action: action)
        popRecognizer.delegate = self
        popRecognizer.maximumNumberOfTouches = 1
        
        let gestureView = gesture?.view
        gestureView?.addGestureRecognizer(popRecognizer)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        get {
            
            if let vc = topViewController {
                return vc.preferredStatusBarStyle
            }
            return UIApplication.shared.statusBarStyle
        }
    }
    
    override var shouldAutorotate: Bool {
        get {
            if let vc = topViewController {
                return vc.shouldAutorotate
            }
            return false
        }
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        get {
            if let vc = topViewController {
                return vc.supportedInterfaceOrientations
            }
            return UIInterfaceOrientationMask.portraitUpsideDown
        }
    }
    
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        get {
            
            if let vc = topViewController {
                return vc.preferredInterfaceOrientationForPresentation
            }
            return UIInterfaceOrientation.portrait
            
        }
    }
    
    override var childViewControllerForStatusBarStyle: UIViewController? {
        get {
            return self.topViewController
        }
    }
    
    override var childViewControllerForStatusBarHidden: UIViewController? {
        get {
            return self.topViewController
        }
    }
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    
    
}

extension LSFNavigationController:UIGestureRecognizerDelegate {
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
       //根页面
        if self.viewControllers.count == 1 {
            return false
        }
        
        //从右往左滑
        if gestureRecognizer is UIPanGestureRecognizer {
            let panGest = gestureRecognizer as! UIPanGestureRecognizer
            let translation = panGest.translation(in: panGest.view)
            if translation.x <= 0  {
                return false
            }
        }
        
    
    return true
    }
}
